#include "../Header Files/CIntroCinematicState.h"
#include "../../Renderer/Header Files/CRenderer_AL.h"
#include "../../Renderer/Header Files/CAssetManager_AL.h"
#include "../../Renderer/Header Files/Direct3D_AL.h"
#include "../Header Files/CMainMenuState.h"


void CIntroCinematicState::Update(float fDT)
{
	//Handle Updating
	m_fTimeElapsed += fDT;
	if (m_fTimeElapsed >= m_fTotalTimeOfCinematic)
	{
		//Go to the Main Menu
		CGame::GetInstance()->PopState();
		return;
	}
	//Handle Input
	Input();

	m_pObjectManager->Update(fDT, m_pCamera);
}
void CIntroCinematicState::Exit()
{
	m_pObjectManager->ClearAllObjects();
	CMainMenuState* state = new CMainMenuState();
	state->Initialize();
	CGame::GetInstance()->PushState(state);
	if (m_pCamera)
	{
		delete m_pCamera;
		m_pCamera = NULL;
	}

}
void CIntroCinematicState::InitializePlayer()
{
	m_pMainPlayer = new CUnit();
	m_pObjectManager->AddItemToObjects((CBaseObject*)m_pMainPlayer);
}

void CIntroCinematicState::Enter()
{
	m_fTimeElapsed = 0.0f;
	m_fTotalTimeOfCinematic = 1.0f;
	if ( !m_pMainPlayer )
		InitializePlayer();
}
void CIntroCinematicState::Initialize()
{
	m_pObjectManager = CObjectManager::GetInstance();
	m_pCamera = new cCamera_AL();
	m_pCamera->BuildPerspective(D3DX_PI / 3, (float)(1024 / 724), 0.1f, 1000.0f);
	m_pCamera->SetViewPosition(0.0f, 0.0f, -30.0f);
	InitializePlayer();
	/*
	int id = CAssetManager_AL::GetInstance()->CreateTexture("stage.png");
	RECT r = { 0, 0, 500, 400 };
	m_pBGSprite = new CSpriteNode_AL(CAssetManager_AL::GetInstance()->GetTexture(id), r, D3DXVECTOR3(200, 200, 0), D3DXVECTOR3(1.1f, 1.1f, 1.1f));
	m_vRenderNodes.push_back((CRenderNode_AL*)m_pBGSprite);

	//*************
	//  Creating the ground. TEST
	D3DXMATRIX pos;
	D3DXMatrixIdentity(&pos);
	D3DXMatrixTranslation(&pos, 0.0f, 0.0f, 4.0f);

	int texID = CAssetManager_AL::GetInstance()->CreateTexture("monopoly-board.png");

	m_pGround = new C3DNode_AL(CAssetManager_AL::GetInstance()->GetTexture(texID), pos, 3);

	D3DVERTEXELEMENT9 grounddecl[] =
	{
		{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
		{ 0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
		D3DDECL_END()
	};
	Direct3D_AL::GetInstance()->m_pD3DDev->CreateVertexDeclaration(grounddecl, &m_pGround->m_vVertDecl);
	CAssetManager_AL::GetInstance()->AddVertDecl(m_pGround->m_vVertDecl);

	//----------------------------
	Verts_PosUV Ground[4];

	Ground[0].Position = D3DXVECTOR3(-25.0f, -2.5f, 25.0f);
	Ground[1].Position = D3DXVECTOR3(25.0f, -2.5f, 25.0f);
	Ground[2].Position = D3DXVECTOR3(25.0f, -2.5f, -25.0f);
	Ground[3].Position = D3DXVECTOR3(-25.0f, -2.5f, -25.0f);

	Ground[0].u = 0.0f;
	Ground[0].v = 0.0f;

	Ground[1].u = 1.0f;
	Ground[1].v = 0.0f;

	Ground[2].u = 1.0f;
	Ground[2].v = 1.0f;

	Ground[3].u = 0.0f;
	Ground[3].v = 1.0f;

	Direct3D_AL::GetInstance()->m_pD3DDev->CreateVertexBuffer(4 * sizeof(Verts_PosUV),
		0, 0,
		D3DPOOL_DEFAULT,
		&m_pGround->m_vVertBuffer, 0);
	void* buffer;
	m_pGround->m_vVertBuffer->Lock(0,
		4 * sizeof(Verts_PosUV),
		&buffer, 0);
	memcpy(buffer, Ground, 4 * sizeof(Verts_PosUV));
	m_pGround->m_vVertBuffer->Unlock();

	CAssetManager_AL::GetInstance()->AddVertBuff(m_pGround->m_vVertBuffer);


	//----------------------

	WORD iList[6];
	iList[0] = 0;
	iList[1] = 2;
	iList[2] = 3;

	iList[3] = 0;
	iList[4] = 1;
	iList[5] = 2;

	Direct3D_AL::GetInstance()->m_pD3DDev->CreateIndexBuffer(6 * sizeof(WORD), 0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &m_pGround->m_vIndexBuffer, 0);
	m_pGround->m_vIndexBuffer->Lock(0,
		6 * sizeof(WORD),
		&buffer, 0);
	memcpy(buffer, iList, 6 * sizeof(WORD));
	m_pGround->m_vIndexBuffer->Unlock();

	CAssetManager_AL::GetInstance()->AddIndexBuff(m_pGround->m_vIndexBuffer);

	m_vRenderNodes.push_back((CRenderNode_AL*)m_pGround);
	//***************************************************************

	*/

}
bool CIntroCinematicState::Input()
{



	if (GetAsyncKeyState('W'))
	{
		m_pCamera->ViewTranslateLocalZ(0.1f);
	}
	if (GetAsyncKeyState('S'))
	{
		m_pCamera->ViewTranslateLocalZ(-0.1f);
	}
	if (GetAsyncKeyState('A'))
	{
		m_pCamera->ViewRotateLocalY(-D3DXToRadian(10.0f) * 0.01f);
	}
	if (GetAsyncKeyState('D'))
	{
		m_pCamera->ViewRotateLocalY(D3DXToRadian(10.0f) * 0.01f);
	}
	//------------------------------------------------------------------

	return true;
}